
var questionrow=1;
var rowcheckboxdataq=1;
var objdeletedatarowq=new Array();
var objdeletedatarowq2=new Array();
var rowdeletedatarowq=0;
var rowdeletedatarowq2=0;
var rowiddatasetchoice=1;
function addformquestionnaire()
{
   $("#addform").append('<div id="feature'+rowdiv+'" class="itPlg addQustPlug featureclass">'+dataform['questionnaire']+"</div>");
   var rownow=rowdiv;


  
  

   
   $('#feature'+rownow+' .deleterowbutton').click(function(){
   
    $('#feature'+rownow).remove();
    
   });
   
   

   $('#feature'+rownow+' .morequestion').click(function(){
   
    morequestiondata(rownow);
    
   });
  
  
 
  
 
  
  
  
  
  
  
  
  
  
  
  
  //$('#feature'+rownow+' .group')
   
   
  
   

  
  
   rowdiv++;
}
function deleterowquestion(rownow,questionrowset,questidset)
{

   $('#rowquestiondatasetdiv-'+questionrowset).remove();
   if(questidset!="")
   {
   
   objdeletedatarowq[rowcheckboxdataq]=questidset;
   rowcheckboxdataq++;
   
   }
   
   
   if($( '#feature'+rownow+" .textnewsetquestion" ).length)
   {
    var rownownewset=1;  
   $( '#feature'+rownow+" .textnewsetquestion" ).each(function( index ) {
//console.log( index + ": " + $( this ).text() );
    
    $(this).html(rownownewset+'. Question Title');
    rownownewset++;
    });
   
   }
   


}
function dataswapmode(questionrowset)
{
var typeset=$('#rowquestiondatasetdiv-'+questionrowset+' select[name="questype2[]"]').val();
$('#rowquestiondatasetdiv-'+questionrowset+' .wrpChoiQust').remove();

switch (typeset)
{

case '1':
  addrowboxdataquestion(questionrowset,'','',1);
  break;
case '2':
  addrowboxdataquestion(questionrowset,'','',0);
  break;
case '3':

  break;
case '4':

  break;
case '5':
  addrowboxdataquestion(questionrowset,'','',2);
  break;
} 


}
function addrowboxdataquestion(questionrowset,squeid,texttitle,statusboxset)
{


 var squeidset='';
      if(squeid)
      {
      
      squeidset=squeid;
      
      }
      
 var texttitleset='';
      if(texttitle)
      {
      
      texttitleset=texttitle;
      
      }


var code = '<tr  class="wrpChoiQust">\n';
code += '<th>Choice</th>\n';
code += '<td class="tdrowdatachoice" >\n';
code += '<div class="choiQust choicheckrow" id="choiQustset-'+rowiddatasetchoice+'">\n';
if(statusboxset==1)
{
code += '<input type="radio" disabled="">\n';
}else if(statusboxset==2)
{
code += '';
}else
{
code += '<input type="checkbox" disabled="">\n';

}
code += '<input type="text" value="'+texttitleset+'" name="titlequestion[]" class="clBg5 clBd2 fs03 clTxt2">\n';
code += '<input type="hidden" name="squeidset" value="'+squeidset+'" class="clBg5 clBd2 fs03 clTxt2">\n';
code +=' <input type="hidden" name="etcset" value="0" >';
code += '<input type="button" onclick="removechoicedataq('+rowiddatasetchoice+')" class="btnGen btnRmv clTxtW cutTxt clBgBt1 fs04" value="X">\n';
code += '</div>\n';

code += '<div class="addChoiQust">\n';

 if(statusboxset!=2)
 {
code += '<input type="button" onclick="addrowradiochoicedata('+questionrowset+','+statusboxset+',2)" class="btnGen clTxtW cutTxt clBgBt1 fs04" value="Add \'Other\'">\n';
 }

code += '<input type="button" onclick="addrowradiochoicedata('+questionrowset+','+statusboxset+',1)" class="btnGen clTxtW cutTxt clBgBt1 fs04" value="Add Choice">\n';
code += '</div>\n';
code += '</td>\n';
code += '</tr>\n';

//console.log(code);
$('#rowquestiondatasetdiv-'+questionrowset+'  tbody').append(code);

rowiddatasetchoice++;


}
function removechoicedataq(rowiddatasetchoiceset,squeid)
{
      var squeidset='';
      if(squeid)
      {
      
      squeidset=squeid;
      
      }

      
   $('#choiQustset-'+rowiddatasetchoiceset).remove();
   if(squeidset!="")
   {
   
   objdeletedatarowq2[rowdeletedatarowq2]=squeidset;
   rowdeletedatarowq2++;
   
   }


}
function getformquestionnaire(arraydata)
{
   

   
var   code  = '<form id="formsqueid-'+arraydata['squeid']+'" name="submitformdata" ><div class="ldIt wrpQust clBd2 bdFull clBg2 squeidclass" >';
   
code += '<div class="expQust" onclick="exportdatareport('+arraydata['productid']+','+arraydata['squeid']+','+arraydata['plugreid']+')">';
code += '<input type="button" class="btnEPRsv clBgBt1" value="">';
code += '</div>';

code += '<div class="ttlQst">';
code += '<span class="clTxt2 fs04 boldGen">'+arraydata['title']+'</span>';
code += '</div>';

code +='<input type="hidden" value="'+arraydata['squeid']+'" name="squeid" >';
code +='<input type="hidden" value="'+arraydata['productid']+'" name="productid" >';
//code +='<input type="hidden" value="'+arraydata['squeid']+'" name="squeid" class="clBg5 clBd2 fs03 clTxt3">';



var krow=1;
if(arraydata['form'].length)
{

$.each( arraydata['form'], function( key, valueform ) {



code += '<div class="ldIt wrpFrmLd clBg4">';
code += '<div class="wrpHdInfoGen">';
code += '<div class="wrpInFrmLd">';
code += '<h2 class="boldGen clTxt3 fs04">'+krow+'. '+valueform['title']+'</h2>';
code += '<div class="wrpFrmInLd">';







switch (parseInt(valueform['typeid'],10)) {
    case 1:
        if(valueform['datarow'].length)
        {
      
        $.each( valueform['datarow'], function( key, valuerow ) {
        
        code += '<div class="itFrmLd radioFrmLd">';
        code += '<input type="radio" value="'+valuerow['squestid']+'" name="answer['+(krow-1)+']" >';
        if(valuerow['etc']=='1')
        {
        
        code += '<label for="rdoOpt1" class="clTxt3 fs03">อื่นๆ</label><input type="text" name="etctext['+valuerow['squestid']+']" class="textother">';
        }else
        {
        code += '<label for="rdoOpt1" class="clTxt3 fs03">'+valuerow['value']+'</label>';
        
        }
        
        
        
        code += '</div>';
        
        
        
        });
        
        
        code += '<input type="hidden" name="questid[]" value="'+valueform['questid']+'" class="clBg5 clBd2 fs03 clTxt3">';
        code += '<input type="hidden" name="typeid[]" value="'+valueform['typeid']+'" class="clBg5 clBd2 fs03 clTxt3">';
        }
        break;
    case 2:
        if(valueform['datarow'].length)
        {
        $.each( valueform['datarow'], function( key, valuerow ) {
     
        
        code += '<div class="itFrmLd radioFrmLd">';
        code += '<input type="checkbox" value="'+valuerow['squestid']+'" name="answer['+(krow-1)+'][]" >';
        if(valuerow['etc']==1)
        {
        
        code += '<label for="rdoOpt1" class="clTxt3 fs03">อื่นๆ</label><input type="text" name="etctext['+valuerow['squestid']+']" class="textother">';
        }else
        {
        code += '<label for="rdoOpt1" class="clTxt3 fs03">'+valuerow['value']+'</label>';
        
        }
        
        
        
        code += '</div>';
        
        
        
        });
        
        
        code += '<input type="hidden" name="questid[]" value="'+valueform['questid']+'" class="clBg5 clBd2 fs03 clTxt3">';
        code += '<input type="hidden" name="typeid[]" value="'+valueform['typeid']+'" class="clBg5 clBd2 fs03 clTxt3">';
        }
        break;
    case 3:
    code += '<div class="itFrmLd inpTxtFrmLd">';
    code += '<input type="text" name="answer['+(krow-1)+']" class="clBg5 clBd2 fs03 clTxt3">';
    code += '<input type="hidden" name="questid[]" value="'+valueform['questid']+'" class="clBg5 clBd2 fs03 clTxt3">';
    code += '<input type="hidden" name="typeid[]" value="'+valueform['typeid']+'" class="clBg5 clBd2 fs03 clTxt3">';
    code += '</div>';
        break;
    case 4:
    
     code += '<div class="itFrmLd txtAreaFrmLd">';
     code += '<textarea name="answer['+(krow-1)+']" class="clBg5 clBd2 fs03 clTxt3"></textarea>';
     code += '<input type="hidden" name="questid[]" value="'+valueform['questid']+'" class="clBg5 clBd2 fs03 clTxt3">';
     code += '<input type="hidden" name="typeid[]" value="'+valueform['typeid']+'" class="clBg5 clBd2 fs03 clTxt3">';
     code += '</div>';

        break;
     case 5:
if(valueform['datarow'].length)
        {
        code += '<div class="itFrmLd dropDownFrmLd"><div class="reDesign"><select class="fs01" name="answer['+(krow-1)+']">';
       // foreach($valueform['datarow'] as $valuerow)
       $.each( valueform['datarow'], function( key, valuerow ) {

        
        
            code +='<option value="'+valuerow['squestid']+'">'+valuerow['value']+'</option>';
        
        
        
        
        
        
        });
        code += '</select></div></div>';
        
        
        code += '<input type="hidden" name="questid[]" value="'+valueform['questid']+'" class="clBg5 clBd2 fs03 clTxt3">';
        code += '<input type="hidden" name="typeid[]" value="'+valueform['typeid']+'" class="clBg5 clBd2 fs03 clTxt3">';
        }
        break;

}


code += '</div>';
code += '</div>';
code += '</div>';
code += '</div>';
krow++;
});

}



code += '<div class="ldIt wrpBtnLd clBg4">';
code += '<div class="wrpBtnQst">';
code += '<div class="booking clBgSp2">';
code += '<span class="clTxtW fs09" onclick="submitanswerform('+arraydata['squeid']+')">SEND</span>';
code += '</div>';
code += '</div>';
code += '</div>';

code += '</div></form>';



  
  return code;
   
   
   }
function addrowradiochoicedata(questionrowset,statusboxset,statuschoice,squeid,texttitle)
{

var squeidset='';
      if(squeid)
      {
      
      squeidset=squeid;
      
      }
var texttitleset='';
      if(texttitle)
      {
      
      texttitleset=texttitle;
      
      }
      


var code="";



if(statuschoice==1)
{

code = '<div class="choiQust choicheckrow" id="choiQustset-'+rowiddatasetchoice+'">\n';
if(statusboxset==1)
{
code += '<input type="radio" disabled="">\n';
}
else if(statusboxset==2)
{

code += '';

}

else
{
code += '<input type="checkbox" disabled="">\n';
}
code += '<input type="text" value="'+texttitleset+'" name="titlequestion[]" class="clBg5 clBd2 fs03 clTxt2">\n';
code += '<input type="hidden" name="squeidset" value="'+squeidset+'" >\n';
code += '<input type="hidden" name="etcset" value="0" >';

code += '<input type="button" onclick="removechoicedataq('+rowiddatasetchoice+',\''+squeidset+'\')" class="btnGen btnRmv clTxtW cutTxt clBgBt1 fs04" value="X">\n';
code += '</div>\n';


}else
{


code = '<div class="choiOthQust choicheckrow"  id="choiQustset-'+rowiddatasetchoice+'">\n';

if(statusboxset==1)
{
code += '<input type="radio" disabled="">\n';
}
else if(statusboxset==2)
{

code += '';

}

else
{
code += '<input type="checkbox" disabled="">\n';
}


code += '<label class="">Other</label>\n';
code += '<input type="text" name="titlequestion[]" readonly="" value="User\'s Answer" class="clBg5 clBd2 fs03 clTxt2">\n';
code += '<input type="button" onclick="removechoicedataq('+rowiddatasetchoice+',\''+squeidset+'\')" value="X" class="btnGen btnRmv clTxtW cutTxt clBgBt1 fs04">\n';
code += '<input type="hidden" name="squeidset" value="'+squeidset+'" >';
code += '<input type="hidden" name="etcset" value="1" >';
code += '</div>';

//code = '<div class="choiOthQust" id="choiQustset-'+rowiddatasetchoice+'">';

//code += '<input type="radio" disabled="">';
//code +='<label class="">Other</label>';
//code += '<input type="text" value="User\'s Answer" readonly="readonly" name="titlequestion[]" class="clBg5 clBd2 fs03 clTxt2">';


//code += '<input type="button" onclick="removechoicedataq('+rowiddatasetchoice+')" class="btnGen btnRmv clTxtW cutTxt clBgBt1 fs04" value="X">';

//code += '</div>\n';


}

$('#rowquestiondatasetdiv-'+questionrowset+' .tdrowdatachoice .addChoiQust').before( code);
rowiddatasetchoice++;



}
function morequestiondata(rownow,questid,text)
{

     var questidset='';
      if(questid)
      {
      questidset=questid;
      
      }
      
      
      var textset='';
      if(text)
      {
      textset=text;
      
      }
      
      
var code = '<div id="rowquestiondatasetdiv-'+questionrow+'" class="tbQst clBd2 bdFull clBg4 rowquestiondataset">\n';
code +='<div class="wrpTl"><div title="Close" onclick="deleterowquestion('+rownow+','+questionrow+',\''+questidset+'\')" class="btnIco closeTl clBd3 mgBtm10"><span class="ico icoCloseTl"></span></div></div>';
code += '<table class="tbGenInfo tbAddQst tbAddCtnt clTxt3 fs03">\n';
code += '<tbody><tr>\n';
code += '<th class="textnewsetquestion">'+questionrow+'. Question Title</th>\n';
code += '<td>\n';
code += '<input name="questitle2[]" value="'+textset+'" type="text" class="clBg5 clBd2 fs03 clTxt2">\n';
code += '<input name="questid2[]" value="'+questidset+'" type="hidden" >\n';
code += '</td>\n';
code += '</tr>\n';

code += '<tr>\n';
code += '<th>Question Type</th>\n';
code += '<td>\n';
code += '<div class="reDesign">\n';
code += '<select name="questype2[]" onchange="dataswapmode('+questionrow+')">\n';
code += '<option value="3">Text</option>\n';
code += '<option value="4">Paragraph Text</option>\n';
code += '<option value="1">Multiple Choice</option>\n';
code += '<option value="2">Checkboxes</option>\n';
code += '<option value="5">Drop Down List</option>\n';
code += '</select>\n';
code += '</div>\n';
code += '</td>\n';
code += '</tr>\n';

//code += '<tr style="display:none" class="wrpChoiQust">\n';
//code += '<th>Choice</th>\n';
//code += '<td>\n';
//code += '<div class="choiQust">\n';
//code += '<input type="radio" disabled="">\n';
//code += '<input type="text" class="clBg5 clBd2 fs03 clTxt2">\n';
//code += '<input type="button" class="btnGen btnRmv clTxtW cutTxt clBgBt1 fs04" value="X">\n';
//code += '</div>\n';
//code += '<div class="addChoiQust">\n';
//code += '<input type="button" class="btnGen clTxtW cutTxt clBgBt1 fs04" value="Add \'Other\'">\n';
//code += '<input type="button" class="btnGen clTxtW cutTxt clBgBt1 fs04" value="Add Choice">\n';
//code += '</div>\n';
//code += '</td>\n';
//code += '</tr>\n';

code += '</tbody></table>\n';
code += '</div>';


$( '#feature'+rownow+" .bntAddQuest" ).before( code);


 if($( '#feature'+rownow+" .textnewsetquestion" ).length)
   {
    var rownownewset=1;  
   $( '#feature'+rownow+" .textnewsetquestion" ).each(function( index ) {
//console.log( index + ": " + $( this ).text() );
    
    $(this).html(rownownewset+'. Question Title');
    rownownewset++;
    });
   
   }

questionrow++;
      


}
function saveformquestionnaire(id,plugid)
{

        var obj2=new Array();
        var krow=0;
        var krow2=0;
        var obj;
       if($("#"+id+' .rowquestiondataset' ).length)
       { 
       $("#"+id+' .rowquestiondataset' ).each(function( index ) {
       //console.log( index + ": " + $( this ).text() );
             
            var obj3=new Array(); 
             krow2=0;
           var typedata= $(this).find('select[name="questype2[]"]').val();
           if(typedata=='1'||typedata=='2'||typedata=='5')
           {
           if($(this).find('.choicheckrow').length)
           {
           
               $(this).find('.choicheckrow').each(function( index ) {
               
               var etcset=0;
               if($(this).find('input[name="etcset"]').val()=='1')
               {
               etcset=1;
               }
               obj3[krow2]={
               squestid:$(this).find('input[name="squeidset"]').val(),
               value:$(this).find('input[name="titlequestion[]"]').val(),
               etc:etcset
               
               
               };
               
               krow2++;
               
               });
           
           
           
           
           }
           else
           {
           
           obj={'code':'22','msg':'Requied Choice Of Question','error':1};
           return obj;
           
           }
           }
             
             
           obj2[krow]={
           typeid:typedata,
           questid:$(this).find('input[name="questid2[]"]').val(),
           title:$(this).find('input[name="questitle2[]"]').val(),
           datarow:obj3
           
           };
           krow++;
       
       });
       
       var obj3=new Array();
       var krow=0;
       
    
       
       
        obj={plugid:plugid,title:$("#"+id+' input[name="questitle[]"]').val(),userquestion:$("#"+id+' input[name="userquestion"]:checked').val(),squeid:$("#"+id+' input[name="squeid[]"]').val(),datarowobj:obj2,deleterowobj:objdeletedatarowq,deleterowobj2:objdeletedatarowq};
       }else
       {
       

        obj={'code':'22','msg':'Requied Question','error':1};

       
       }
     //   console.log(obj);
    //    alert('');
        
  //     obj={'code':'22','msg':'Requied Question','error':1};

        return obj;
}
function submitanswerform(squeid)
{

var parem=new Array();
loaddata();
$.post(serviceinfo+'postmedthodplugin', $('#formsqueid-'+squeid).serialize()+'&classset=questionnaire&medthodset=submitanswerform'  , function(data) { 
//loadclosedata();
if(data.error.response)
{
loadclosedata();   
if(data.error.error[0].code==5)
{
$('.ovlPopLd').hide();
$('.wrpPopLI').fadeIn();

//console.log('test');
}else
{
//alertbox(data.error.error);

alertbox(data.error.error);
}

}
else
{
loadclosedata();   
boxmsgdata('Done');

}


});


}
function editformquestionnaire(datarow)
{
addformquestionnaire();
var rownow= rowdiv-1;

$('#feature'+rownow+' input[name="questitle[]"]').val(datarow.title);
$('#feature'+rownow+' input[name="squeid[]"]').val(datarow.squeid);

var $radios = $('#feature'+rownow+' input[name="userquestion"]');
   // if($radios.is(':checked') === false) {
$radios.filter('[value='+datarow.userquestion+']').prop('checked', true);


if(datarow.form.length)
{
   
$.each( datarow.form, function( key, valueform ) {

morequestiondata(rownow,valueform['questid'],valueform['title']);

var questionrownow=questionrow-1;
$('#feature'+rownow+' #rowquestiondatasetdiv-'+questionrownow+' select[name="questype2[]"]').val(valueform['typeid']);

if(valueform.typeid=="1")
{
if(valueform.datarow.length)
{
var firstdata=1;
$.each( valueform.datarow, function( key2, valueform2 ) {

if(firstdata==1)
{
firstdata=0;
addrowboxdataquestion(questionrownow,valueform2['squestid'],valueform2['value'],1);

}else
{

var rowstatusset=1;
if(valueform2['etc']=='1')
{
      rowstatusset=0;
}

addrowradiochoicedata(questionrownow,1,rowstatusset,valueform2['squestid'],valueform2['value']);
}


});

}

}
else if(valueform.typeid=="2")
{

if(valueform.datarow.length)
{
var firstdata=1;
$.each( valueform.datarow, function( key2, valueform2 ) {

if(firstdata==1)
{
firstdata=0;
addrowboxdataquestion(questionrownow,valueform2['squestid'],valueform2['value'],0);

}else
{

var rowstatusset=1;
if(valueform2['etc']=='1')
{
      rowstatusset=0;
}

addrowradiochoicedata(questionrownow,0,rowstatusset,valueform2['squestid'],valueform2['value']);
}


});

}


}
else if(valueform.typeid=="5")
{

if(valueform.datarow.length)
{
var firstdata=1;
$.each( valueform.datarow, function( key2, valueform2 ) {

if(firstdata==1)
{
firstdata=0;
addrowboxdataquestion(questionrownow,valueform2['squestid'],valueform2['value'],2);

}else
{

var rowstatusset=1;
if(valueform2['etc']=='1')
{
      rowstatusset=0;
}

addrowradiochoicedata(questionrownow,2,rowstatusset,valueform2['squestid'],valueform2['value']);
}


});

}


}



});

}

//console.log(datarow);



}
function exportdatareport(productid,squeid,plugreid)
{



location.href=serviceinfo+'getmedthodplugin?classset=questionnaire&medthodset=exportdatareport&parem[]='+productid+'&parem[]='+squeid+'&parem[]='+plugreid+'&nojson=1';



}
